package com.mvity.framework.common.base;

import java.io.Serializable;
import java.util.Date;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import com.mvity.framework.common.global.IResponseOutput;
import com.mvity.framework.common.utils.JsonUtil;
import com.mvity.framework.common.utils.XmlUtil;

/**
 * 视图对象 基类
 * 
 * @author vity
 * @date 2012-3-15
 * @mail vity.1215@gmail.com
 */
public abstract class BaseVO implements Serializable, Comparable<BaseVO>, IResponseOutput {

	private static final long serialVersionUID = -6585107919099973415L;

	protected static final Logger logger = LoggerFactory.getLogger(BaseVO.class);

	public BaseVO() {
	}

	public static <T extends BaseEntity, E extends BaseVO> E fromEntity(Class<E> clazz, T entity) {
		E vo = null;
		try {
			vo = clazz.newInstance();
			BeanUtils.copyProperties(entity, vo);
		} catch (InstantiationException e) {
			logger.error(e.getMessage());
		} catch (IllegalAccessException e) {
			logger.error(e.getMessage());
		}
		return vo;
	}

	@Override
	public String toJsonString() {
		return JsonUtil.beanToJson(this);
	}

	@Override
	public String toXmlString() {
		return XmlUtil.beanToXML(this);
	}

	/**
	 * 数据表主键ID
	 */
	private String id;

	/**
	 * 数据创建时间
	 */
	private Date createTime;

	/**
	 * 数据最近更新时间
	 */
	private Date lastModifyTime;

	/**
	 * 数据是否删除状态
	 */
	private String isDelete;

	/**
	 * 预留数据列
	 */
	private String reserveColumn;

	/**
	 * 权重索引列
	 */
	private Integer orderIndex;

	/**
	 * 数据项状态
	 */
	private String dataState;

	/**
	 * 所属租户
	 */
	private String tenantId;

	/**
	 * 创建用户
	 */
	private String createUserId;

	/**
	 * 修改用户
	 */
	private String modifyUserId;

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * @return the createTime
	 */
	public Date getCreateTime() {
		return createTime;
	}

	/**
	 * @param createTime
	 *            the createTime to set
	 */
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	/**
	 * 返回 lastModifyTime 的值
	 * 
	 * @return lastModifyTime
	 */
	public Date getLastModifyTime() {
		return lastModifyTime;
	}

	/**
	 * 设置 lastModifyTime 的值
	 * 
	 * @param lastModifyTime
	 */
	public void setLastModifyTime(Date lastModifyTime) {
		this.lastModifyTime = lastModifyTime;
	}

	/**
	 * @return the isDelete
	 */
	public boolean isDelete() {
		return Boolean.valueOf(this.isDelete);
	}

	/**
	 * @param isDelete
	 *            the isDelete to set
	 */
	public void setDelete(Boolean isDelete) {
		this.isDelete = String.valueOf(isDelete);
	}

	/**
	 * @return the reserveColumn
	 */
	public String getReserveColumn() {
		return reserveColumn;
	}

	/**
	 * @param reserveColumn
	 *            the reserveColumn to set
	 */
	public void setReserveColumn(String reserveColumn) {
		this.reserveColumn = reserveColumn;
	}

	/**
	 * 返回 orderIndex 的值
	 * 
	 * @return orderIndex
	 */
	public Integer getOrderIndex() {
		return orderIndex;
	}

	/**
	 * 设置 orderIndex 的值
	 * 
	 * @param orderIndex
	 */
	public void setOrderIndex(Integer orderIndex) {
		this.orderIndex = orderIndex;
	}

	/**
	 * 返回 dataState 的值
	 * 
	 * @return dataState
	 */
	public String getDataState() {
		return dataState;
	}

	/**
	 * 设置 dataState 的值
	 * 
	 * @param dataState
	 */
	public void setDataState(String dataState) {
		this.dataState = dataState;
	}

	/**
	 * @return the tenantId
	 */
	public String getTenantId() {
		return tenantId;
	}

	/**
	 * @param tenantId
	 *            the tenantId to set
	 */
	public void setTenantId(String tenantId) {
		this.tenantId = tenantId;
	}

	/**
	 * @return the createUserId
	 */
	public String getCreateUserId() {
		return createUserId;
	}

	/**
	 * @param createUserId
	 *            the createUserId to set
	 */
	public void setCreateUserId(String createUserId) {
		this.createUserId = createUserId;
	}

	/**
	 * @return the modifyUserId
	 */
	public String getModifyUserId() {
		return modifyUserId;
	}

	/**
	 * @param modifyUserId
	 *            the modifyUserId to set
	 */
	public void setModifyUserId(String modifyUserId) {
		this.modifyUserId = modifyUserId;
	}

	/*
	 * 依据 orderIndex 属性进行降序排列
	 * 
	 * @param o
	 * 
	 * @return
	 * 
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	@Override
	public int compareTo(BaseVO o) {
		return o.getOrderIndex().compareTo(this.getOrderIndex());
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		BaseVO other = (BaseVO) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

}
